package com.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.config.Msg;
import com.pojo.*;
import com.server.AssistServer;
import com.server.MaterialServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
@CrossOrigin
@RestController
public class AssistController {
    private final AssistServer assistServer;
    private final MaterialServer materialServer;
    @Autowired
    public AssistController(AssistServer assistServer,MaterialServer materialServer) {
        this.assistServer = assistServer;
        this.materialServer = materialServer;
    }

    @RequestMapping("/select/Assist")
    @ResponseBody
    public List<Assist> selectAssist(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String cpyt = jsonObject.getString("cpyt");
        String fabh = jsonObject.getString("fabh");
        cpyt = (cpyt != null) ? cpyt : "";
        fabh = (fabh != null) ? fabh : "";
        List<Assist> assists = assistServer.selectAssist(limit, page, cpyt, fabh);
        return assists;
    }

    @RequestMapping("/add/Assist")
    @ResponseBody
    public Msg addMaterial(@RequestBody JSONObject jsonObject) throws ParseException {
        Msg msg = new Msg();
        String cpyt = jsonObject.getString("cpyt");
        String kh = jsonObject.getString("kh");
        String cz = jsonObject.getString("cz");
        BigDecimal hd = jsonObject.getBigDecimal("hd");
        BigDecimal kd = jsonObject.getBigDecimal("kd");
        String cslx = jsonObject.getString("cslx");
        String csfs = jsonObject.getString("csfs");
        String dwjs = jsonObject.getString("dwjs");
        String ywy = jsonObject.getString("ywy");
        String ywygh = jsonObject.getString("userName");
        String ywbm = jsonObject.getString("ywbm");
        String ywbmgh = jsonObject.getString("ywbmgh");
        String zdr = jsonObject.getString("zdr");
        String bz = jsonObject.getString("bz");
        String zdbm = jsonObject.getString("zdbm");
        String bzfabh = jsonObject.getString("bzfabh");
        String bzfamc = jsonObject.getString("bzfamc");
        String zdsjStr = jsonObject.getString("zdsj");
        String djrqStr = jsonObject.getString("djrq");
        BigDecimal ddsl = jsonObject.getBigDecimal("ddsl");
        zdsjStr = zdsjStr.replace("/", "-");
        djrqStr = djrqStr.replace("/", "-");

        int id = assistServer.getAssist()+1;
        String fabh = String.valueOf(assistServer.getAssist() + 80000000);
        int i = assistServer.addAssist(id, fabh, djrqStr, 0, cslx, cpyt, kh, cz, hd, kd, csfs, dwjs, ddsl, ywy, ywygh, ywbm, ywbmgh, bz, zdr, zdbm, zdsjStr, "", "", "", "",bzfabh,bzfamc);
        JSONArray requestDataArray = jsonObject.getJSONArray("requestData");
        int i2 = 1;
        for (int j = 0; j < requestDataArray.size(); j++) {
            JSONObject item = requestDataArray.getJSONObject(j);
            int  assistlList = assistServer.getAssistList() + 1;
            String wlbm = item.getString("wlbm");
            String wlmc = item.getString("wlmc");
            String wlgg = item.getString("wlgg");
            String jldw = item.getString("jldw");
            BigDecimal bzsl = item.getBigDecimal("bzsl");
            BigDecimal bzdj = item.getBigDecimal("bzdj");
            BigDecimal bzje = item.getBigDecimal("bzje");
            BigDecimal zs = item.getBigDecimal("zs");
            BigDecimal je = item.getBigDecimal("je");
            String bzList = item.getString("bz1");
            if (wlbm.isEmpty() || wlmc.isEmpty()) {
                break;
            }
            int i1 = assistServer.addAssistList(assistlList, wlbm, wlmc, wlgg, jldw, bzsl, bzdj, bzje, ddsl, zs, je, bzList, csfs, dwjs, id);
            if (i1 == 0) {
                i2 = 0;
            }
        }
        if (i > 0 && i2 > 0) {
            msg.setCode(200);
            msg.setMessage("添加成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("添加失败");
        return msg;
    }
    @RequestMapping("/select/AssistList")
    @ResponseBody
    public List<AssistList> selectMaterialList(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        List<AssistList> assistLists = assistServer.selectAssistList(id);
        return assistLists;
    }

    @RequestMapping("/update/Assist")
    @ResponseBody
    public Msg updateMaterial(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");
        String cslx = jsonObject.getString("cslx");
        String cpyt = jsonObject.getString("cpyt");
        String kh = jsonObject.getString("kh");
        String cz = jsonObject.getString("cz");
        BigDecimal hd = jsonObject.getBigDecimal("hd");
        BigDecimal kd = jsonObject.getBigDecimal("kd");
        String csfs = jsonObject.getString("csfs");
        String dwjs = jsonObject.getString("dwjs");
        String bz = jsonObject.getString("bz");
        String fabh = jsonObject.getString("fabh");
        BigDecimal ddsl = jsonObject.getBigDecimal("ddsl");
        String bzfabh = jsonObject.getString("bzfabh");
        String bzfamc = jsonObject.getString("bzfamc");
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        List<Department> departments = materialServer.selectDepartment(users.get(0).getUserId());
        String bmmc = departments.get(0).getBmmc();
        int i = assistServer.updateAssist(cslx, cpyt, kh, cz, hd, kd, csfs, dwjs, bz, name, bmmc, fabh, ddsl,bzfabh,bzfamc);
        List<Assist> assists = assistServer.selectAst(fabh);
        int i1 = assistServer.deleteAssistList(assists.get(0).getId());
        JSONArray requestDataArray = jsonObject.getJSONArray("requestData");
        int i2 = 1;
        // 遍历数组
        for (int j = 0; j < requestDataArray.size(); j++) {
            JSONObject item = requestDataArray.getJSONObject(j);
            int  assistlList = assistServer.getAssistList() + 1;
            String wlbm = item.getString("wlbm");
            String wlmc = item.getString("wlmc");
            String wlgg = item.getString("wlgg");
            String jldw = item.getString("jldw");
            BigDecimal bzsl = item.getBigDecimal("bzsl");
            BigDecimal bzdj = item.getBigDecimal("bzdj");
            BigDecimal bzje = item.getBigDecimal("bzje");
            BigDecimal zs = item.getBigDecimal("zs");
            BigDecimal je = item.getBigDecimal("je");
            String bzList = item.getString("bz1");
            if (wlbm.isEmpty() || wlmc.isEmpty()) {
                break;
            }
            int i3 = assistServer.addAssistList(assistlList, wlbm, wlmc, wlgg, jldw, bzsl, bzdj, bzje, ddsl, zs, je, bzList, csfs, dwjs, assists.get(0).getId());

            if (i3 == 0) {
                i2 = 0;
            }
        }
        if (i > 0 && i2 > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }
   //删除
   @RequestMapping("/delete/Assist")
   @ResponseBody
   public Msg deleteMaterial(@RequestBody JSONObject jsonObject) {
       int id = jsonObject.getIntValue("id");
       String userName = jsonObject.getString("userName");
       Msg msg = new Msg();
       Assist assist = assistServer.selectDjzt(id);
       int djzt = assist.getDjzt();
       if (djzt > 0) {
           msg.setCode(500);
           msg.setMessage("该单据已审核，不能删除");
           return msg;
       }
       int i = assistServer.deleteAssist(id);
       int i1 = assistServer.deleteAssistList(id);
       if (i > 0) {
           msg.setCode(200);
           msg.setMessage("删除成功");
           return msg;
       }
       msg.setCode(500);
       msg.setMessage("删除失败");
       return msg;
   }
    @RequestMapping("/up/as/auto0")
    @ResponseBody
    public Msg upASauto0(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getIntValue("id");
        String userName = jsonObject.getString("userName");
        String shsj = jsonObject.getString("shsj");
        shsj = shsj.replace("/", "-");
        Msg msg = new Msg();
        Assist assist = assistServer.selectDjzt(id);
        int djzt = assist.getDjzt();
        System.out.println(djzt);
        if (djzt>0){
            msg.setCode(500);
            msg.setMessage("单据请勿重复审核");
            return msg;
        }
        int i = assistServer.intupDateAuto0(id, userName, shsj);

        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("审核成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("审核失败");
        return msg;
    }

    @RequestMapping("/up/as/auto1")
    @ResponseBody
    public Msg upASauto1(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getIntValue("id");
        String userName = jsonObject.getString("userName");
        String shsj = jsonObject.getString("shsj");
        shsj = shsj.replace("/", "-");
        Msg msg = new Msg();
        Assist assist = assistServer.selectDjzt(id);
        int djzt = assist.getDjzt();
        if (djzt==0){
            msg.setCode(500);
            msg.setMessage("单据请勿重复弃审");
            return msg;
        }
        int i = assistServer.intupDateAuto1(id, userName, shsj);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("弃审成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("弃审失败");
        return msg;
    }
}
